package patrol.mobile.com.mobilepatrol.activity.yanghuke.zuoyefenbu;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.google.gson.Gson;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.adapter.ZuoYeFenbuAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.DingweiBean;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.AlwaysMarqueeTextView;

/**
 * Created by z on 2018/9/8.
 * Des: 作业分布Activity
 */

public class ZuoyeFenBuActivity extends AppCompatActivity implements View.OnClickListener, CallBack<DateBean> {
    @BindView(R.id.timeContent)
    TextView timeContent;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_center_title)
    AlwaysMarqueeTextView tvCenterTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.map)
    MapView mMapView;
    @BindView(R.id.map2)
    MapView mMapView2;
    @BindView(R.id.lin_2)
    LinearLayout lin_2;
    @BindView(R.id.iv_end_title)
    ImageView ivEndTitle;
    @BindView(R.id.titleAddIcon)
    ImageView titleAddIcon;

    private AMap aMap;
    private AMap aMap2;
    private ZuoYeFenbuAdapter zuoYeFenbuAdapter;
    private DingweiBean dingweiBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zuoyefenbu_layout);
        ButterKnife.bind(this);
        aMap = mMapView.getMap();
        aMap2 = mMapView2.getMap();
        ivBack.setVisibility(View.VISIBLE);
        tvCenterTitle.setVisibility(View.VISIBLE);
        titleAddIcon.setVisibility(View.VISIBLE);
        mMapView.onCreate(savedInstanceState);
        mMapView2.onCreate(savedInstanceState);
        timeContent.setOnClickListener(this);
        timeContent.setText(TimeUtils.dateString(System.currentTimeMillis()));
        ivBack.setOnClickListener(this);
        tvCenterTitle.setText("作业分布");
        titleAddIcon.setOnClickListener(this);
        titleAddIcon.setImageResource(R.mipmap.iv_loc_title);
        CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(39.463102471870286, 75.98912333977705), 12, 30, 0));
        CameraUpdateFactory.zoomTo(12);
        aMap.animateCamera(mCameraUpdate);
        aMap2.animateCamera(mCameraUpdate);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(zuoYeFenbuAdapter = new ZuoYeFenbuAdapter());
        zuoYeFenbuAdapter.setOnLocationListener(new ZuoYeFenbuAdapter.OnLocationListener() {
            @Override
            public void onclick(int postion) {
                String olat = zuoYeFenbuAdapter.getDingweiBean().getList().get(postion).getOlat();
                String olng = zuoYeFenbuAdapter.getDingweiBean().getList().get(postion).getOlng();
                try {
                    if (!TextUtils.isEmpty(olat)&&!TextUtils.isEmpty(olng)){
                        CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(Double.parseDouble(olng), Double.parseDouble(olat)), 12, 30, 0));
                        CameraUpdateFactory.zoomTo(12);
                        aMap.animateCamera(mCameraUpdate);
                        aMap2.animateCamera(mCameraUpdate);
                    }else{
                        ToastUtils.showToast("无经纬度信息,无法查看!");
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }

            }
        });
        getHttpData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.timeContent:
                DatePickerUtil.getInstance(this).setCallBack(this);
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.titleAddIcon:
                if (lin_2.getVisibility() == View.GONE) {
                    lin_2.setVisibility(View.VISIBLE);
                    mMapView2.setVisibility(View.GONE);
                } else {
                    lin_2.setVisibility(View.GONE);
                    mMapView2.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

    /**
     * 获取机构
     */
    private void getHttpData() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("period", timeContent.getText().toString());
        String baseUrl = (String) SPUtils.get(Constants.current_ser, "");
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getYhkOperationAreaMapList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                dingweiBean = new Gson().fromJson(response, DingweiBean.class);
                zuoYeFenbuAdapter.setDingweiBean(dingweiBean);
                setMakers(dingweiBean);
            }
        });
    }

    //设置覆盖物
    private void setMakers(DingweiBean dingweiBean) {
        for (DingweiBean.ListBean listBean : dingweiBean.getList()) {
            if (!TextUtils.isEmpty(listBean.getOlat()) && !TextUtils.isEmpty(listBean.getOlng())) {
                try {
                    LatLng latLng = new LatLng(Double.parseDouble(listBean.getOlng()), Double.parseDouble(listBean.getOlat()));
                    Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).title("路线:"+listBean.getRoutelinename().concat("线")+"\n桩号:"+listBean.getPot().concat("\n工作内容:")+listBean.getWorksInfo()).snippet(listBean.getSituation()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 时间选择回调
     */
    @Override
    public void onBack(DateBean dateBean) {
        timeContent.setText(dateBean.getmDate());
        getHttpData();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
        mMapView2.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
        mMapView2.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
        mMapView2.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
        mMapView2.onSaveInstanceState(outState);
    }
}
